package main

import (
	"encoding/json"
	"wxgo/log"
	"wxgo/util"
)

// wxAccessToken 获取微信access_token
func wxAccessToken() {
	p := map[string]string{
		"grant_type": "client_credential",
		"appid":      appid,
		"secret":     appsecret,
	}

	data, err := util.Get(wxURLAccessToken, nil, p)
	if err != nil {
		log.Error("获取access_token失败,原因：%s", err.Error())
		return
	}

	json.Unmarshal(data, &r)
	if t, ok := r["access_token"]; ok {
		token = t.(string)
		log.Debug("access token is: ", token)
	} else {
		log.Debug("errcode is : %d, visit for help: https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_access_token.html", int(r["errcode"].(float64)))
	}
}

// wxCreateMenus 创建公众号下方菜单
func wxCreateMenus() {
	menuJSON := `
	{
		"button":[
			{
				"type":"view",
				"name": "报告查询",
				"key":"report",
				"url":"http://www.qdinfo.top/gzh/"
			}
		]
	}
	`

	link := wxURLCustomMenu + "?access_token=" + token
	data, err := util.PostJSON(link, menuJSON)
	if err != nil {
		log.Error("创建自定义菜单失败, 原因：%s", err.Error())
		return
	}

	json.Unmarshal(data, &r)
	log.Debug("创建自定义菜单，返回码 ", r["errcode"], " 错误：", r["errmsg"])
}

func main() {
	wxAccessToken()

	wxCreateMenus()
}
